Skip to content

Design Patterns#

Classic Gang of Four design patterns with Python implementations for every pattern. Interview design data structure problems (LRU, TimeMap, etc.) also live in Challenge Yourself.

When to use design patterns#

Pattern family Solves Interview frequency
Creational Object creation complexity Medium — Singleton, Factory
Structural Composition and wrappers Medium — Adapter, Decorator, Facade
Behavioural Communication and responsibility High — Observer, Strategy, Iterator

Pages in this section#

Page Patterns covered
Creational Singleton, Factory Method, Abstract Factory, Builder, Prototype
Structural Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy
Behavioural Observer, Strategy, Command, Iterator, State, Memento, and more

Each page includes complete Python code, explanations, and interview notes for every pattern in that family.

Interview design problems (Python)#

FAANG design a data structure questions map to patterns + DSA Patterns:

LeetCode Pattern(s) Solution
146 LRU Cache Hash Map + Linked List Challenge Yourself §1
460 LFU Cache Hash Map + Linked List + Greedy Challenge Yourself §2
380 Insert Delete GetRandom O(1) Hash Map + Array Challenge Yourself §3
981 Time Based Key-Value Store Hash Map + Binary Search Challenge Yourself §4
295 Find Median from Data Stream Two Heaps Challenge Yourself §9
173 BST Iterator Iterator / Stack Tree § BST Iterator

Python-first

All GoF patterns below have runnable Python solutions. For LeetCode-style design a class questions, see Challenge Yourself.

Pattern → problem mapping (study order)#

  1. Singleton — config, logging (Creational)
  2. Factory / Builder — object creation with many options (Creational)
  3. Adapter / Facade — simplify external APIs (Structural)
  4. Decorator — add behavior without subclass explosion (Structural)
  5. Strategy — swap algorithms at runtime (Behavioural)
  6. Observer — pub/sub, event systems (Behavioural)
  7. Iterator — traverse without exposing internals → BST Iterator, LRU order